package cn.stu.system.controller;


import cn.stu.common.utils.ApiData;
import cn.stu.common.utils.UserHolder;
import cn.stu.framework.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 考勤控制器
 *
 * @author HGZS
 * @date 2023/08/06
 * @since 2020/12/20
 */
@RestController
@RequestMapping("/attendances")
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;


    /**
     * 出席名单
     *
     * @param year  一年
     * @param month 月
     * @param day   一天
     * @return {@link ApiData}
     */
    @GetMapping("/list")
    public ApiData listAttendance(@RequestParam("year") Integer year, @RequestParam("month") Integer month, @RequestParam(value = "day", required = false) Integer day) {
        Long userId = UserHolder.getUser().getUserId();
        return attendanceService.listByUserIdAndClockDate(year, month, day, userId);
    }

    /**
     * 打卡
     *
     * @return {@link ApiData}
     */
    @PostMapping("/clockIn")
    public ApiData clockIn() {
        Long userId = UserHolder.getUser().getUserId();
        return attendanceService.create(userId);
    }

    /**
     * 打卡
     *
     * @param id id
     * @return {@link ApiData}
     */
    @PutMapping("/clockOut")
    public ApiData clockOut(@RequestParam Long id) {
        Long userId = UserHolder.getUser().getUserId();
        return attendanceService.update(id, userId);
    }

}